package models

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)"json:"username"`
	Password string `gorm:"type:varchar(225)"json:"password"`
}

type Userrloegxb struct {
	gorm.Model
	Uid int `gorm:"type:char(11)"json:"uid"`
	Rid int `gorm:"type:char(11)"json:"rid"`
}

type Role struct {
	gorm.Model
	Rolename string `gorm:"type:varchar(30)"json:"rolename"`
}

type Rolequangxb struct {
	gorm.Model
	Qid int `gorm:"type:char(11)"json:"qid"`
	Rid int `gorm:"type:char(11)"json:"rid"`
}

type Quan struct {
	gorm.Model
	Quanname string `gorm:"type:varchar(30)"json:"quanname"`
}

type Compan struct {
	gorm.Model
	Companyname string `gorm:"type:varchar(30)"json:"companyname"`
}

type Types struct {
	gorm.Model
	Typesname string `gorm:"type:varchar(30)"json:"typesname"`
}

type Orders struct {
	gorm.Model
	Ordername string `gorm:"type:varchar(30)"json:"ordername"`
	Cid       int    `gorm:"type:char(11)"json:"cid"`
	Tid       int    `gorm:"type:char(11)"json:"tid"`
}

func (*User) Dengss(username string, user *User) error {
	return DB.Where("username=?", username).First(&user).Error
}
func (r *Rolequangxb) DataAdd(data *Rolequangxb) error {
	tx := DB.Begin()
	err := tx.Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (r *Role) RoleList(uid int) error {
	err := DB.Table("roles").
		Joins("LEFT JOIN userrloegxbs ON userrloegxbs.rid = roles.id").
		Where("userrloegxbs.uid = ?", uid).
		Find(&r).Error
	return err
}
func (q *Quan) QuanList(rid int) error {
	err := DB.Table("quans").
		Joins("LEFT JOIN rolequangxbs ON rolequangxbs.qid = quans.id").
		Where("rolequangxbs.rid = ?", rid).
		Find(&q).Error
	return err
}
func (r *Compan) AddData(data *Compan) error {
	tx := DB.Begin()
	err := tx.Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
func (r *Orders) AddDatass(data *Orders) error {
	tx := DB.Begin()
	err := tx.Create(&data).Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
